<?PHP

if( simpax_archive_posts() ) :
	include "addons/clean_archives.php";
endif;

/*
 * Give all linked images the rel="lightbox[list]" for lightbox
 */

if( simpax_lightbox() == "slim" ) :

	function simpax_rel_lightbox($content){
		return preg_replace('/<a(.*?)href=(.*?).(jpg|jpeg|png|gif|bmp|ico)"(.*?)>/i', '<a$1href=$2.$3" $4 rel="lightbox[list]">', $content);
	}
	add_filter('the_content', 'simpax_rel_lightbox', 2);

elseif( simpax_lightbox() == "one" ) :

	function simpax_rel_lightbox($content){
		return preg_replace('/<a(.*?)href=(.*?).(jpg|jpeg|png|gif|bmp|ico)"(.*?)>/i', '<a$1href=$2.$3" $4 rel="lightbox">', $content);
	}
	add_filter('the_content', 'simpax_rel_lightbox', 2);
	
elseif( simpax_lightbox() == "thick" ) :

	function simpax_rel_lightbox($content){
		if( preg_match('/<a(.*?)href=(.*?).(jpg|jpeg|png|gif|bmp|ico)"(.*?)class="(.*?)"(.*?)>/i', $content) ) :
			return preg_replace('/<a(.*?)href=(.*?).(jpg|jpeg|png|gif|bmp|ico)"(.*?)class="(.*?)"(.*?)>/i', '<a$1href=$2.$3"$4class="$5 thickbox" rel="gallery-cax" $6>', $content);
		else :
			return preg_replace('/<a(.*?)href=(.*?).(jpg|jpeg|png|gif|bmp|ico)"(.*?)>/i', '<a$1href=$2.$3" $4 class="thickbox" rel="gallery-cax">', $content);
		endif;
	}
	add_filter('the_content', 'simpax_rel_lightbox', 2);

endif;

/*
 * Add Lightbox-Code to Zooomr/Flickr-Thumbnails with link to Zooomr/Flickr
 *
 
if( simpax_is_zooomr_lightbox() ) :
	
	function simpax_zooomr_lightbox($content){
		if( preg_match('
			/<a(.*?)href="(.*?)(zooomr|flickr)(.*?)">/<img(.*?)scr=(.*?)(zooomr|flickr)(.*?)(_m|_b|_t|_o|_s)(.*?)"(.*?)alt="(.*?)"(.*?)>/</a>',
			$content) ) 
		{
			preg_replace(
				'/<a(.*?)href="(.*?)(zooomr|flickr)(.*?)">/<img(.*?)scr=(.*?)(zooomr|flickr)(.*?)(_m|_b|_t|_o|_s)(.*?)"(.*?)alt="(.*?)"(.*?)>/</a>', 
				'/<a$1href="$6$7$8_b$10"
					onmousedown=\"this.title=\'<a href=\\\'$2$3$4\\\'>$12<\\/a>\';\" onmouseout=\"this.title=\'\'\"
				>/<img$5scr="$6$7$8$10$9"$11alt="$12"$13>/</a>', 
				$content);
		}
		add_filter('the_content', 'simpax_zooomr_lightbox', 2);
	}

endif;
*/
if( simpax_is_zooomr_lightbox() ) :
	
	function simpax_zooomr_lightbox($content){
		if( preg_match('
			/\<a href =(  .*?)>(.*?)\<\/a\>/si',
			$content) ) 
		{
			preg_replace(
				"/\<a href=(.*?)>(.*?)\<\/a\>/si","<a href=\'$1\' onmousedown=\"this.title=\\\'$1\\\';\" onmouseout=\"this.title=\'\'\">$2</a>", 
				$content);
		}
		add_filter('the_content', 'simpax_zooomr_lightbox', 2);
	}

endif;

/*
 * PAGEBAR (here called pagenav, from http://elektroelch.de/hacks/wp/pagebar)
 */
 
/* --------------------------------------------------------------------------------------------- */
function showPagenavTooltip($tooltip, $tooltipText, $page)
{
  if ($tooltip)
    return ' title="' . $tooltipText . ' ' . $page .'"';
}  
/* --------------------------------------------------------------------------------------------- */

function pagenav($params)
{
	global $request, $posts_per_page, $wpdb, $paged, $wp_pagebar, $wp_version;
	
	/* Handle default values */
	if (! isset($params['before']))          { $params['before']       = ''; }
  if (! isset($params['after']))           { $params['after']        = ''; }	
  if (! isset($params['tooltip']))         { $params['tooltip']      = true; }		
  if (! isset($params['tooltip_text']))    { $params['tooltip_text'] = 'Page'; }		
  if (! isset($params['prev']))            { $params['prev']         = '&#9668;'; }	
  if (! isset($params['next']))            { $params['next']         = '&#9658;'; }	
  if (! isset($params['directions']))      { $params['directions']   = true; }	
	
	
	if (!is_single()) 
	{
		if ($wp_version < "2.1")
		  preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
		else
		  preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);
		$fromwhere = $matches[1];
		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
		$max_page = ceil($numposts /$posts_per_page);
		
		
		if(empty($paged)) 
			$paged = 1;

		if($max_page > 1) 
		{
			
			echo '<div class="pagebar">' . $params['before'];
			
			if ($paged>1 && $params['directions'])
        echo '<a href="'.trim(get_pagenum_link($paged-1)).'" '.showPagenavTooltip($params['tooltip'],$params['tooltip_text'], $paged-1).'>'.$params['prev'].'</a> ';
			
			if ($paged >= 4) 
				echo '<a href="'.get_pagenum_link().'"'.showPagenavTooltip($params['tooltip'],$params['tooltip_text'], "1").'>1</a> <span class="break">...</span> ';
			
			for($i = $paged - 2 ; $i  <= $paged +2; $i++) {
				if ($i >= 1 && $i <= $max_page) {
					if($i == $paged) 
						echo '<span class="this-page">'.$i.'</span>';
					else 
						echo ' <a href="'.get_pagenum_link($i).'"'.showPagenavTooltip($params['tooltip'],$params['tooltip_text'], $i) .'>'.$i.'</a> ';
				}
			}
			if (($paged+2) < ($max_page)) 
				echo ' <span class="break">...</span> <a href="'.get_pagenum_link($max_page).'"'.showPagenavTooltip($params['tooltip'],$params['tooltip_text'], $max_page).'>'.$max_page.'</a>';

      if ($paged<$max_page && $params['directions'])
        echo ' <a href="'.get_pagenum_link($paged+1).'"'.showPagenavTooltip($params['tooltip'],$params['tooltip_text'], $paged+1).'>'.$params['next'].'</a>';

			echo $params['after'] . '</div>';
		}
	}
}

/*
 * Season Style Switcher
 * Plugin URI: http://cfkone.net/
 * Description: Switches a couple of CSS-Files in dependance of different time periods.
*/


if( simpax_styleswitcher() ) : // only load it if we need it

	require_once("addons/propertyreader.php");
	require_once("addons/datetimeutil.php");

	/**
	 * Switches the style-sheets by time defined in switcher.xml
	 * @param $path - the path to the style-sheet directory. Default: wordpress default
	 */
	function seasonStyle($path=""){
		if(!isset($path) || empty($path)){
			$path = get_bloginfo("stylesheet_directory");
		}
		$propertyReader = PropertyReader::getInstance("$path/plugins/switcher.xml");
		
		$config = $propertyReader->getConfig();
		$defaultStyle = $config["DEFAULT-STYLE"];
		
		$seasons = $config["SEASONS"];
		$specialDays = $config["SPECIAL-DAYS"];
		$actYear = date("Y", time());

		// 1st look, if today is a special day...		
		while( list($key, $val) = each($specialDays)){
			list($month, $day) = split("/",$val["DATE"]);
			$checkDate = mktime(0, 0, 0, $month, $day, $actYear);
			if( DateTimeUtil::isToday($checkDate) ){
				$todayStyle = $val["STYLE-SHEET"]["SRC"];
			}
		}
		
		// ...if not, then it may a day within a season period
		if(!isset($todayStyle) || empty($todayStyle)){
			while( list($key, $val) = each($seasons)){
				list($beginMonth, $beginDay) = split("/",$val["BEGIN"]);
				list($endMonth, $endDay) = split("/",$val["END"]);
				$beginDate = mktime(0, 0, 0, $beginMonth, $beginDay, $actYear);
				$endYear = $endMonth < $beginMonth ? $actYear + 1 : $actYear;
				$endDate = mktime(0, 0, 0, $endMonth, $endDay, $endYear); 
				
				$actTime = time();
				if( DateTimeUtil::isInPeriod($actTime, $beginDate, $endDate) ){
					$todayStyle = $val["STYLE-SHEET"]["SRC"];
				}
			}
		}
		
		// ...if today is also not within a season period, then take default style 
		if(!isset($todayStyle) || empty($todayStyle)){
			$todayStyle = $defaultStyle;
		}
		
		echo "$path/$todayStyle";
	}
	
endif;
	
	
/*
 * rndAvatars
 */

// Return opposite of strstr
if(!function_exists(backstr)) :
function backstr($haystack, $needle) {
       return substr($haystack, 0, strlen($haystack) - strlen(strstr($haystack,$needle)));
}
endif;

/*
if(simpax_is_ranvatar()) :
// Return random image code 
// --> this is simpax_ranvatar*S*
function simpax_ranvatars() {

  // Set up variables
  $caxdir = dirname(__FILE__);
  $image_dir = simpax_ranvatar();

  // Get a list of images
  $dirhandle = opendir("$caxdir/ranvatar/$image_dir");
  while (false !== ($filename = readdir($dirhandle))) {
    $image_list[] = $filename;
  }

  // Drop parent and current directories
  do { $image_key = array_rand($image_list, 1);
  } while  (($image_list[$image_key]==".") or ($image_list[$image_key]==".."));

  $image_choice = $image_list[$image_key];
  
  // Output the chosen image along with tags
  echo "<img class='gravatar' src='$image_dir$image_choice' alt='".str_replace("_"," ",backstr($image_choice, "."))."' border='0' />";
}
endif;
*/
?>